module(..., package.seeall)

function new(sound)
	require "sprite"
	--local ui = require("ui")
	local tableView = require("tableView")
	--local widget = require "widget"

	local menuGroup = display.newGroup()
	local waitingGroup
	
	local tapSound = audio.loadSound( "tapsound.wav" )
	local soundOnOff = sound
	
	local StoreName = ""
	local storeid = -1
	
	local storeIds = {}
	local data = {
		data = {}
	}
	---------------------------------------------
	--init screen
	---------------------------------------------
	local drawScreen = function()
		bg = display.newImage("images/bgMain.png")
		bg.x = display.contentWidth/2
		bg.y = display.contentHeight/2
		menuGroup:insert(bg)
		
		AppName = display.newImage("images/AppName.png")
		AppName.x = display.contentWidth/2
		AppName.y = AppName.height/2 + 10
		menuGroup:insert(AppName)
		
		ChooseStore = display.newImage("images/ChooseStore.png")
		ChooseStore.x = display.contentWidth/2
		ChooseStore.y = ChooseStore.height/2 + AppName.height + 5
		menuGroup:insert(ChooseStore)
	end
	---------------------------------------------
	--
	---------------------------------------------
	function onComplete(event)
		loadData()
	end
	---------------------------------------------
	--event network
	---------------------------------------------
	function networkListener( event )
		if theTimer then
			timer.cancel(theTimer)
		end
		if waitingGroup then
			for i = waitingGroup.numChildren,1,-1 do
				local child = waitingGroup[i]
				child:removeSelf()
				child = nil
			end  
			waitingGroup:removeSelf()
			waitingGroup = nil
		end
		if (event.isError) then
			native.showAlert( "", "Please check your internet connect!", { "OK"}, onComplete )
		else
			local result = event.response
			
			if result == "NULL" then
				director:changeScene("menuplay", emaillogin)
			else
				--result = "1|Cua hang 1@STORE2|Cua hang 2"
				local k = 1
				local i, j = string.find(result, "@STORE", k)
				local index = 1
				if i == nil then
					local si = result
					local ii, jj = string.find(si, "|", 1)
					if ii and jj then
						data[index] = {}
						data[index].id = si:sub(1, ii - 1)
						data[index].title = si:sub(ii + 1)
						
						print(data[index].title)
					end
				else
					h = 1
					while i ~= nil do
						local si = result:sub(k, i - 1)
						local ii, jj = string.find(si, "|", 1)
						data[index] = {}
						data[index].id = si:sub(1, ii - 1)
						data[index].title = si:sub(ii + 1)
						print(data[index].title)
						k = j + 1
						i, j = string.find(result, "@STORE", k)
						if i == nil then
							si = result:sub(k)
							ii, jj = string.find(si, "|", 1)

							data[index+1] = {}
							data[index+1].id = si:sub(1, ii - 1)
							data[index+1].title = si:sub(ii + 1)
							print(data[index+1].title)
						end
						
						index = index + 1
						h = h + 1
					end
				end

				--Init listview
				local topBoundary = display.screenOriginY + 94
				local bottomBoundary = 5
				myList = tableView.newList{
					data = data, 
					default = "images/item.png",
					over = "images/itemOver.png",
					onRelease = listButtonRelease,
					top = topBoundary,
					bottom = bottomBoundary,
					
					nil,
					callback=function(row, id) 
						local group = display.newGroup()
						--print(id)
						if id == 1 then
							local top = display.newImage("images/item_tops.png")
							top.x = display.contentWidth/2
							top.y = top.height/2
							group:insert(top)
						elseif id == #data then
							local bottom = display.newImage("images/item_bottoms.png")
							bottom.x = display.contentWidth/2
							bottom.y = bottom.height/2
							group:insert(bottom)
						else
							local mid = display.newImage("images/item_mids.png")
							mid.x = display.contentWidth/2
							mid.y = mid.height/2
							group:insert(mid)
						end
						
						local t = display.newText(row.title, 0, 0, native.systemFontBold, 18)
						t:setTextColor(0, 0, 0)
						t.x = 26 + t.width/2
						t.y = 27
						t:setReferencePoint(display.TopLeftReferencePoint)
						group:insert(t)
						
						local nextIcon = display.newImage("images/next.png")
						nextIcon.x = display.contentWidth - 24
						nextIcon.y = 27
						group:insert(nextIcon)
						
						group.x = 480
						transition.to(group, {time=500, x=0, transition=easing.outExpo})

						return group
					end
			}
			menuGroup:insert(myList)
			
			AppName:toFront()
			end
		end
	end
	---------------------------------------------
	--event when selected item on the list
	---------------------------------------------
	function listButtonRelease( event )
		self = event.target
		director:changeScene("menustyle", soundOnOff, data[self.id].title, data[self.id].id)
	end
	---------------------------------------------
	--load data form server
	---------------------------------------------
	function loadData()
		if waitingGroup == nil then
			waitingGroup = display.newGroup()
			menuGroup:insert(waitingGroup)
			
			Waiting = display.newImage("images/waiting.png")
			Waiting.x = display.contentWidth/2
			Waiting.y = display.contentHeight*.5
			Waiting.alpha = 0.3
			waitingGroup:insert(Waiting)
			
			theTimer = timer.performWithDelay(20, 
			function(event)
				Waiting.rotation = Waiting.rotation + 5
				if Waiting.rotation > 360 then
					Waiting.rotation = 0
				end
			end, 0)
			
			local url = "http://" .. serverhost .. "/wswines.ashx?action=GSTORES"
			network.request(url, "GET", networkListener )
		end
	end
	
	drawScreen()
	
	loadData()
	
	unloadMe = function()
	end
	
	return menuGroup
end